﻿using B.S.DataBase.MyProject.Domain.RBAC;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Menus;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Menus
{
    public class DelMenuCommandHandler:IRequestHandler<DelMenuCommand,APIResult<object>>
    {
        private readonly IBaseRepository<Menu> menuRep;

        public DelMenuCommandHandler(IBaseRepository<Menu> menuRep)
        {
            this.menuRep = menuRep;
        }

        public async Task<APIResult<object>> Handle(DelMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var obj = menuRep.GetAll().Result.Where(x => x.MenuId == request.MenuId || x.PId == request.MenuId).ToList();
            var res = menuRep.DeleteRanges(obj).Result;
            if (res > 0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res;
            }
            return await Task.FromResult(result);
        }
    }
}
